using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using UnityEngine;

public class GameRecorderPlayer : MonoBehaviour
{
	private string filePath;

	private string assetPath;

	public bool loadFromResources;

	public bool resumeAfterPlay = true;

	private List<Vector3> recordPosition;

	private List<Quaternion> recordRotation;

	private List<Vector3> recordVelocity;

	private List<Vector3> recordAngularVelocity;

	private List<int> recordTouchFrames;

	private List<Vector3> recordTouchStarts;

	private List<Vector3> recordTouchEnds;

	private List<float> recordTouchDurations;

	public bool isAvailable;

	public bool isPlaying;

	private int recordFrame;

	private int currentStepTouches;

	private bool haveTouches;

	private void Start()
	{
	}

	private void OnEnable()
	{
		Init();
	}

	private void OnDisable()
	{
		isPlaying = false;
		isAvailable = false;
		recordPosition = null;
		recordRotation = null;
		recordVelocity = null;
		recordAngularVelocity = null;
		recordTouchFrames = null;
		recordTouchStarts = null;
		recordTouchEnds = null;
		recordTouchDurations = null;
	}

	private void Init()
	{
		isPlaying = false;
		isAvailable = false;
		filePath = "GameRecording_" + Game.currentLevelName;
		filePath = filePath.Replace(".", string.Empty) + ".xml";
		filePath = Application.persistentDataPath + "/" + filePath;
		assetPath = "Recordings/GameRecording_Tutorial" + Game.currentLevelNumber.ToString("00");
	}

	private void FixedUpdate()
	{
		if (!isPlaying)
		{
			return;
		}
		BallBehaviour.GetInstance().SetManualData(recordPosition[recordFrame], recordRotation[recordFrame], recordVelocity[recordFrame], recordAngularVelocity[recordFrame]);
		recordFrame++;
		if (haveTouches && recordFrame + 4 == recordTouchFrames[currentStepTouches])
		{
			Vector3 startPos = recordTouchStarts[currentStepTouches];
			Vector3 endPos = recordTouchEnds[currentStepTouches];
			float duration = recordTouchDurations[currentStepTouches];
			TutorialInterface.ShowSwipe(startPos, endPos, duration);
			currentStepTouches++;
			if (currentStepTouches == recordTouchFrames.Count)
			{
				haveTouches = false;
			}
		}
		if (recordFrame == recordPosition.Count)
		{
			isPlaying = false;
			if (resumeAfterPlay)
			{
				BallBehaviour.GetInstance().ResumeMovement();
			}
		}
	}

	public void Play(bool restartLevel = true, bool refreshRecord = true)
	{
		if (refreshRecord)
		{
			RefreshRecord();
		}
		if (isAvailable)
		{
			if (restartLevel)
			{
				Game.Respawn(true, 0f);
			}
			isPlaying = true;
			recordFrame = 0;
			currentStepTouches = 0;
			haveTouches = recordTouchFrames.Count > 0;
		}
	}

	public void Stop()
	{
		isPlaying = false;
		BallBehaviour.GetInstance().ResumeMovement();
	}

	public void RefreshRecord()
	{
		isAvailable = false;
		recordPosition = new List<Vector3>();
		recordRotation = new List<Quaternion>();
		recordVelocity = new List<Vector3>();
		recordAngularVelocity = new List<Vector3>();
		recordTouchFrames = new List<int>();
		recordTouchStarts = new List<Vector3>();
		recordTouchEnds = new List<Vector3>();
		recordTouchDurations = new List<float>();
		bool flag = false;
		XmlDocument xmlDocument = new XmlDocument();
		try
		{
			if (loadFromResources)
			{
				TextAsset textAsset = Resources.Load(assetPath) as TextAsset;
				if (textAsset != null)
				{
					xmlDocument.LoadXml(textAsset.text);
					flag = true;
				}
			}
			else
			{
				xmlDocument.Load(filePath);
				flag = true;
			}
		}
		catch (IOException ex)
		{
			Debug.Log("GameRecorderPlayer -> RefreshRecord - no record found (IOException) : " + ex.Message);
		}
		catch (IsolatedStorageException ex2)
		{
			Debug.Log("GameRecorderPlayer -> RefreshRecord - no record found (IsolatedStorageException) : " + ex2.Message);
		}
		catch
		{
			Debug.Log("GameRecorderPlayer -> RefreshRecord - no record found (Exception UNKNOWN)");
		}
		if (!flag)
		{
			return;
		}
		XmlNode documentElement = xmlDocument.DocumentElement;
		foreach (XmlNode childNode in documentElement.ChildNodes)
		{
			if (childNode.Name == "BallData")
			{
				foreach (XmlNode item in childNode)
				{
					recordPosition.Add(Utils.StringToVector3(item.Attributes["pos"].Value));
					recordRotation.Add(Utils.StringToQuaternion(item.Attributes["rot"].Value));
					recordVelocity.Add(Utils.StringToVector3(item.Attributes["vel"].Value));
					recordAngularVelocity.Add(Utils.StringToVector3(item.Attributes["avel"].Value));
				}
			}
			else
			{
				if (!(childNode.Name == "TouchData"))
				{
					continue;
				}
				foreach (XmlNode item2 in childNode)
				{
					recordTouchFrames.Add(int.Parse(item2.Attributes["f"].Value));
					recordTouchStarts.Add(Utils.StringToVector3(item2.Attributes["posS"].Value));
					recordTouchEnds.Add(Utils.StringToVector3(item2.Attributes["posE"].Value));
					recordTouchDurations.Add(float.Parse(item2.Attributes["dur"].Value));
				}
			}
		}
		Debug.Log("GameRecorderPlayer -> RefreshRecord - record found : frames = " + recordPosition.Count + ", touches = " + recordTouchFrames.Count);
		xmlDocument.RemoveAll();
		xmlDocument = null;
		isAvailable = true;
	}
}
